package com.aps.apspda.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.aps.apspda.R;
import com.aps.apspda.activity.ThingChangeActivity;
import com.aps.apspda.callback.DeleteCallback;
import com.aps.apspda.dialog.DeleteFragmentDialog;
import com.aps.apspda.dialog.GeneralPop;
import com.aps.apspda.entity.MaterialInfoBean;
import com.aps.apspda.utils.AntiShake;
import com.aps.apspda.utils.AppUtils;
import com.aps.apspda.utils.StaticMembers;
import com.aps.apspda.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author
 * @date 2017/9/11
 * @desc
 */

public class EqpThingListAdapter extends BaseQuickAdapter<MaterialInfoBean, BaseViewHolder> {
    private boolean isShow = true;
    private DeleteCallback deleteCallback;
    private FragmentManager fragmentManager;
    private boolean isSeqNoPullDown = false;

    public EqpThingListAdapter(@Nullable List<MaterialInfoBean> data, DeleteCallback deleteCallback) {
        super(R.layout.adapter_eqp_thing_list, data);
        this.deleteCallback = deleteCallback;
    }

    public void setIsShowButton(boolean isShow) {
        this.isShow = isShow;
    }

    public void setIsSeqNoPullDown() {
        this.isSeqNoPullDown = true;
    }

    public void setFragmentManager(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
    }


    @Override
    protected void convert(final BaseViewHolder helper, final MaterialInfoBean item) {
        helper.getView(R.id.btnChange).setVisibility(isShow ? View.VISIBLE : View.GONE);
        helper.getView(R.id.btnDelete).setVisibility(isShow ? View.VISIBLE : View.GONE);
        final String type = item.getType();
        helper.setText(R.id.tvTitle, AppUtils.getTypeZH(type));
        if (type.equals("HubBlade") || type.equals("HubBladeYZ1") || type.equals("HubBladeYZ2")) {
            helper.getView(R.id.llHubBlade).setVisibility(View.VISIBLE);
            helper.getView(R.id.llWafer).setVisibility(View.GONE);
            helper.getView(R.id.llCapillary).setVisibility(View.GONE);
            helper.getView(R.id.llOther).setVisibility(View.GONE);
            helper.getView(R.id.llExpoxy).setVisibility(View.GONE);
            helper.getView(R.id.llHublessBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWire).setVisibility(View.GONE);
            helper.getView(R.id.llBlock).setVisibility(View.GONE);
            helper.getView(R.id.llLeadframe).setVisibility(View.GONE);
            helper.setText(R.id.tvHubBladeMaterialPartNo, item.getMaterialPartNo());
            helper.setText(R.id.tvHubBladeSawLength, item.getSawLength());
            helper.setText(R.id.tvHubBladeSeqNo, item.getSeqNo());
            helper.setText(R.id.tvHubBladeThickness, item.getSawThickness());
        } else if (type.equals("Wafer")) {
            helper.getView(R.id.llHubBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWafer).setVisibility(View.VISIBLE);
            helper.getView(R.id.llCapillary).setVisibility(View.GONE);
            helper.getView(R.id.llOther).setVisibility(View.GONE);
            helper.getView(R.id.llExpoxy).setVisibility(View.GONE);
            helper.getView(R.id.llHublessBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWire).setVisibility(View.GONE);
            helper.getView(R.id.llBlock).setVisibility(View.GONE);
            helper.getView(R.id.llLeadframe).setVisibility(View.GONE);
            helper.setText(R.id.tvWaferDevice, item.getDevice());
            helper.setText(R.id.tvWaferWaferSource, item.getWaferSource());
            helper.setText(R.id.tvWaferDiagram, item.getDiagram());
            helper.setText(R.id.tvWaferMaterialPartNo, item.getMaterialPartNo());
        } else if (type.equals("Leadframe")) {
            helper.getView(R.id.llHubBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWafer).setVisibility(View.GONE);
            helper.getView(R.id.llCapillary).setVisibility(View.GONE);
            helper.getView(R.id.llOther).setVisibility(View.GONE);
            helper.getView(R.id.llExpoxy).setVisibility(View.GONE);
            helper.getView(R.id.llHublessBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWire).setVisibility(View.GONE);
            helper.getView(R.id.llBlock).setVisibility(View.GONE);
            helper.getView(R.id.llLeadframe).setVisibility(View.VISIBLE);
            helper.setText(R.id.tvLeadframeSeqNo, item.getSeqNo());
            /*helper.setText(R.id.tvLeadframeStartTime, item.getStartTime());*/
            helper.setText(R.id.tvLeadframeMaterialPartNo, item.getMaterialPartNo());
            helper.setText(R.id.tvLeadframeEndTime, item.getEndTime());
        } else if (type.equals("Capillary")) {
            helper.getView(R.id.llHubBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWafer).setVisibility(View.GONE);
            helper.getView(R.id.llCapillary).setVisibility(View.VISIBLE);
            helper.getView(R.id.llOther).setVisibility(View.GONE);
            helper.getView(R.id.llExpoxy).setVisibility(View.GONE);
            helper.getView(R.id.llHublessBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWire).setVisibility(View.GONE);
            helper.getView(R.id.llBlock).setVisibility(View.GONE);
            helper.getView(R.id.llLeadframe).setVisibility(View.GONE);
            helper.setText(R.id.tvCapillaryID, item.getSeqNo());
            helper.setText(R.id.tvCapillaryMaterialPartNo, item.getMaterialPartNo());
        } else if (type.equals("Collet") || type.equals("RubberTip") ||
                type.equals("EjectorPin") || type.equals("StampingPin") || type.equals("DispenseTool")) {
            helper.getView(R.id.llHubBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWafer).setVisibility(View.GONE);
            helper.getView(R.id.llCapillary).setVisibility(View.GONE);
            helper.getView(R.id.llOther).setVisibility(View.VISIBLE);
            helper.getView(R.id.llExpoxy).setVisibility(View.GONE);
            helper.getView(R.id.llHublessBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWire).setVisibility(View.GONE);
            helper.getView(R.id.llBlock).setVisibility(View.GONE);
            helper.getView(R.id.llLeadframe).setVisibility(View.GONE);
            EditText tvOtherSeqNoRequest = helper.getView(R.id.tvOtherSeqNoRequest);
            final TextView tvOtherSeqNoChoose = helper.getView(R.id.tvOtherSeqNoChoose);
            TextView tvOtherSeqNo = helper.getView(R.id.tvOtherSeqNo);

            if (StaticMembers.isOffLine){
                if (isSeqNoPullDown){
                    tvOtherSeqNoRequest.setVisibility(View.VISIBLE);
                    tvOtherSeqNoChoose.setVisibility(View.GONE);
                    tvOtherSeqNo.setVisibility(View.GONE);
                    setEditListener(tvOtherSeqNoRequest, item);
                }else {
                    tvOtherSeqNoRequest.setVisibility(View.GONE);
                    tvOtherSeqNoChoose.setVisibility(View.GONE);
                    tvOtherSeqNo.setVisibility(View.VISIBLE);
                    helper.setText(R.id.tvOtherSeqNo, item.getSeqNo());
                }
            }else {
                if (isSeqNoPullDown) {
                    final String otherStr1 = "请选择;" + item.getSeqNo();
                    item.setSeqNo("请选择");
                    tvOtherSeqNoRequest.setVisibility(View.GONE);
                    tvOtherSeqNoChoose.setVisibility(View.VISIBLE);
                    tvOtherSeqNo.setVisibility(View.GONE);
                    tvOtherSeqNoChoose.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            LayoutInflater inflater = LayoutInflater.from(mContext);
                            View popView = inflater.inflate(R.layout.popupwindow_down_list_select, null);
                            RecyclerView lvDownList = popView.findViewById(R.id.lvDownList);
                            lvDownList.setLayoutManager(new LinearLayoutManager(mContext));
                            lvDownList.setItemAnimator(new DefaultItemAnimator());
                            final PopDownAreaListAdapter popDownAreaListAdapter = new PopDownAreaListAdapter(new ArrayList<String>());
                            popDownAreaListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
                            popDownAreaListAdapter.isFirstOnly(false);
                            popDownAreaListAdapter.setCurrentSelect(0);
                            lvDownList.setAdapter(popDownAreaListAdapter);
                            final GeneralPop titlePop = new GeneralPop(mContext, popView
                                    , tvOtherSeqNoChoose.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
                            String[] strings1 = otherStr1.split(";");
                            final List<String> list1 = Arrays.asList(strings1);
                            popDownAreaListAdapter.setNewData(list1);
                            popDownAreaListAdapter.setOnItemClickListener(new OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    popDownAreaListAdapter.setCurrentSelect(position);
                                    popDownAreaListAdapter.notifyDataSetChanged();
                                    tvOtherSeqNoChoose.setText(list1.get(position));
                                    item.setSeqNo(list1.get(position));
                                    titlePop.dismiss();
                                }
                            });
                            titlePop.showAsDropDown(tvOtherSeqNoChoose, 0, 0);
                        }
                    });
                } else {
                    tvOtherSeqNoRequest.setVisibility(View.GONE);
                    tvOtherSeqNoChoose.setVisibility(View.GONE);
                    tvOtherSeqNo.setVisibility(View.VISIBLE);
                    helper.setText(R.id.tvOtherSeqNo, item.getSeqNo());
                }
            }
            helper.setText(R.id.tvOtherMaterialPartNo, item.getMaterialPartNo());
            helper.setText(R.id.tvOtherLiftTime, item.getThawEndTime());
        } else if (type.equals("Epoxy") || type.equals("EpoxyLeft") || type.equals("EpoxyRight")) {
            helper.getView(R.id.llHubBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWafer).setVisibility(View.GONE);
            helper.getView(R.id.llCapillary).setVisibility(View.GONE);
            helper.getView(R.id.llOther).setVisibility(View.GONE);
            helper.getView(R.id.llExpoxy).setVisibility(View.VISIBLE);
            helper.getView(R.id.llHublessBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWire).setVisibility(View.GONE);
            helper.getView(R.id.llBlock).setVisibility(View.GONE);
            helper.getView(R.id.llLeadframe).setVisibility(View.GONE);
            helper.setText(R.id.tvExpoxyMaterialPartNo, item.getMaterialPartNo());
            helper.setText(R.id.tvExpoxyThawEndTime, item.getThawEndTime());
            helper.setText(R.id.tvExpoxySeqNo, item.getSeqNo());
            helper.setText(R.id.tvExpoxyEndTime, item.getEndTime());
        } else if (type.equals("Paste") || type.equals("LeftPaste") || type.equals("RightPaste")) {
            helper.getView(R.id.llHubBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWafer).setVisibility(View.GONE);
            helper.getView(R.id.llCapillary).setVisibility(View.GONE);
            helper.getView(R.id.llOther).setVisibility(View.GONE);
            helper.getView(R.id.llExpoxy).setVisibility(View.VISIBLE);
            helper.getView(R.id.llHublessBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWire).setVisibility(View.GONE);
            helper.getView(R.id.llBlock).setVisibility(View.GONE);
            helper.getView(R.id.llLeadframe).setVisibility(View.GONE);
            helper.setText(R.id.tvExpoxyMaterialPartNo, item.getMaterialPartNo());
            helper.setText(R.id.tvExpoxyThawEndTime, item.getThawEndTime());
            helper.setText(R.id.tvExpoxySeqNo, item.getSeqNo());
            helper.setText(R.id.tvExpoxyEndTime, item.getEndTime());
        } else if (type.equals("HublessBlade")) {
            helper.getView(R.id.llHubBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWafer).setVisibility(View.GONE);
            helper.getView(R.id.llCapillary).setVisibility(View.GONE);
            helper.getView(R.id.llOther).setVisibility(View.GONE);
            helper.getView(R.id.llExpoxy).setVisibility(View.GONE);
            helper.getView(R.id.llHublessBlade).setVisibility(View.VISIBLE);
            helper.getView(R.id.llWire).setVisibility(View.GONE);
            helper.getView(R.id.llBlock).setVisibility(View.GONE);
            helper.getView(R.id.llLeadframe).setVisibility(View.GONE);
            helper.setText(R.id.tvHublessBladeID, item.getMaterialPartNo());
            helper.setText(R.id.tvHublessBladeExternalDiameter, item.getExternalDiameter());
            helper.setText(R.id.tvHublessBladeInternalDiameter, item.getInternalDiameter());
            helper.setText(R.id.tvHublessBladeThickness, item.getSawThickness());
            helper.setText(R.id.tvHublessBladeSeqNo, item.getSeqNo());
        } else if (type.equals("Wire")) {
            helper.getView(R.id.llHubBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWafer).setVisibility(View.GONE);
            helper.getView(R.id.llCapillary).setVisibility(View.GONE);
            helper.getView(R.id.llOther).setVisibility(View.GONE);
            helper.getView(R.id.llExpoxy).setVisibility(View.GONE);
            helper.getView(R.id.llHublessBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWire).setVisibility(View.VISIBLE);
            helper.getView(R.id.llBlock).setVisibility(View.GONE);
            helper.getView(R.id.llLeadframe).setVisibility(View.GONE);
            helper.setText(R.id.tvWireID, item.getSeqNo());
            helper.setText(R.id.tvWireMaterialPartNo, item.getMaterialPartNo());
            helper.setText(R.id.tvWireSize, item.getSize());
            helper.setText(R.id.tvWireTime, item.getRemainTime());
            helper.setText(R.id.tvWireEndTime, item.getEndTime());
        } else if (type.equals("Block")) {
            helper.getView(R.id.llHubBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWafer).setVisibility(View.GONE);
            helper.getView(R.id.llCapillary).setVisibility(View.GONE);
            helper.getView(R.id.llOther).setVisibility(View.GONE);
            helper.getView(R.id.llExpoxy).setVisibility(View.GONE);
            helper.getView(R.id.llHublessBlade).setVisibility(View.GONE);
            helper.getView(R.id.llWire).setVisibility(View.GONE);
            helper.getView(R.id.llBlock).setVisibility(View.VISIBLE);
            helper.getView(R.id.llLeadframe).setVisibility(View.GONE);
            helper.setText(R.id.tvBlockMaterialPartNo, item.getMaterialPartNo());
        }
        helper.getView(R.id.btnChange).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ThingChangeActivity.class);
                intent.putExtra("mEntity", item);
                intent.putExtra("eqpID", item.getEqpID());
                //intent.putExtra("eqpType", item.getEqpType());
                mContext.startActivity(intent);
            }
        });

        helper.getView(R.id.btnDelete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AntiShake.check(v.getId())) {    //判断是否多次点击
                    ToastUtils.showFreeToast("请勿重复点击",
                            mContext, false, Toast.LENGTH_SHORT);
                    return;
                }
                final DeleteFragmentDialog deleteFragmentDialog = new DeleteFragmentDialog();
                deleteFragmentDialog.setData("确定要删除吗？", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (AntiShake.check(v.getId())) {    //判断是否多次点击
                            ToastUtils.showFreeToast("请勿重复点击",
                                    mContext, false, Toast.LENGTH_SHORT);
                            return;
                        }
                        deleteFragmentDialog.dismiss();
                        deleteCallback.onSuccess(item);
                    }
                });
                deleteFragmentDialog.show(fragmentManager, "delete_dialog");
            }
        });
    }

    private void setEditListener(EditText editText, final MaterialInfoBean item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setSeqNo(s.toString());
            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
